 $j(function() {
	setUpPagingLinks();
	doPagingLink(location);
	setUpJqueryLightbox();
	$j('#prodContainer').ajaxComplete( function () {
		setUpPagingLinks();
		selectTab();
		setUpLightbox();
	});
});

function setUpJqueryLightbox() {
    $j('a[@rel*=JQlightbox]').lightBox({
		overlayBgColor: '#3B89C9',
		overlayOpacity: 0.6,
		imageBtnClose: '../media.threadless.com/imgs/holiday/close_x.gif'
	}); // Select all links that contains lightbox in the attribute rel
}

function setUpLightbox() {
	initialize();
}
function setUpPagingLinks() { 
	$j('a[@href*=index]').each(function(i) {
		var baseUrl = location.href.split('#');
		baseUrl = baseUrl[0];
		var segments = this.href.split('/');
		var hString = baseUrl + '#/';
		var addSize = true;
		for (var i = 0; i < segments.length; i++)
		{
			if(segments[i].indexOf(',') > 0){
				hString += segments[i] + '/';
			}
		}
		this.href = hString;
		this.onclick = function () {
			doPagingLink(this);
		};
	});
}

function doMainTabLinks(size, type){
	$j('.nav li a').each(function(i) {
		if (this.id == 'gcs' || this.id == 'guide'){
			return;
		}
		var baseUrl = location.href.split('#');
		baseUrl = baseUrl[0];
		var segments = this.href.split('/');
		var hString = baseUrl + '#/';
		var addSize = true;
		for (var i = 0; i < segments.length; i++)
		{
			if(segments[i].indexOf(',') > 0)
			{
				var p = segments[i];
				p = p.split(',');
				if (p[0] != 'size' && p[0] != 'type'){
					hString += segments[i] + '/';
				}
				if(p[0] == 'style' && p[1] == 'prints'){
					addSize = false;
				}
			}
		}
		if (size && type && addSize){
			hString += 'size,' + size + '/';
			hString += 'type,' + type + '/';
		}
		this.href = hString;
		this.onclick = function () {
			doPagingLink(this);
		};
	});
	
}

function doPagingLink(caller) { 
	var baseUrl = caller.href.split('#');
	if(baseUrl.length < 2){
		return;
	}
	var segments = baseUrl[1].split('/');
	var qString = '?';
	var sizeParam = false;
	var typeParam = false;
	for (var i = 0; i < segments.length; i++)
	{
		if(segments[i].indexOf(',') > 0)
		{
			var p = segments[i];
			p = p.split(',');
			qString += p[0] + '=' + p[1] + '&';
			if (p[0] == 'size'){
				sizeParam = p[1];
			}
			if (p[0] == 'type'){
				typeParam = p[1];
			}
		}
	}
	$j('#prodContainer').load('../media.threadless.com/scripts/catalog.php' + qString);
	doMainTabLinks(sizeParam, typeParam);
}

function setCurrentTab(id){
	$j('.nav li a').removeClass('cur');
	$j( '#' + id + ' a' ).addClass('cur');
}

function setFilter(id) {
	$j('#' + id + ' a' ).addClass( 'cur' );
}

function selectTab(){
	var baseUrl = location.href.split('#');
	if(baseUrl[1].indexOf('style,hoodies') > 0){
		setCurrentTab('hoodies');		
	} else if (baseUrl[1].indexOf('style,prints') > 0){
		setCurrentTab('prints');		
	} else if (baseUrl[1].indexOf('price,5') > 0){
		setCurrentTab('five');		
	} else if (baseUrl[1].indexOf('price,10') > 0){
		setCurrentTab('ten');		
	} else {
		setCurrentTab('fifteen');		
	}

}
